package model

import (
	"jsjb/connect"
)

type OrgCate struct {
	ID         int32  `json:"id"`
	Name       string `json:"name"`
	Townid     int32  `json:"townId"`
	OfficeId   string `json:"officeId"`
	OfficeName string `json:"officeName"`
	OfficeType int32  `json:"officeType"`
}

func (OrgCate) TableName() string {
	return "org_cate"
}
func FindOrgCate(orgId int32) (o *OrgCate, err error) {
	o = new(OrgCate)
	result := connect.DB.Where("id = ?", orgId).Find(&o)
	if result.Error != nil {
		err = result.Error
	}
	return
}
func ColumnsOrgCate(orgIds []int32) (ns []string, err error) {
	result := connect.DB.Where("id IN ?", orgIds).Pluck("name", &ns)
	if result.Error != nil {
		err = result.Error
	}
	return
}
func SelectOrgCate(orgIds []int32) (os []OrgCate, err error) {
	result := connect.DB.Where("id IN (?)", orgIds).Find(&os)
	if result.Error != nil {
		err = result.Error
	}
	return
}
func RecallByOrgId(orgId int32) (o OrgCate, err error) {
	if err = connect.DB.Raw(`SELECT
	o.townid,
	o.officeId office_id,
	r.officeName office_name,
	r.officeType office_type 
FROM
	org_cate o
	LEFT JOIN recall_staff r ON o.officeId = r.officeId 
WHERE
	o.officeId = (
	SELECT
		officeId 
	FROM
		org_cate 
WHERE
	id = (CASE (SELECT	townid FROM org_cate WHERE id = ? )
		
		WHEN 0 THEN
		1 ELSE (
			SELECT
				townid 
			FROM
				org_cate 
			WHERE
				id = ? 
			)
END)
)`, orgId, orgId).Scan(&o).Error; err != nil {
		return
	}
	return
}
